package cn.adkii.mybatis.session;

import cn.adkii.mybatis.binding.MapperRegistry;
import cn.adkii.mybatis.mapping.MappedStatement;

import java.util.HashMap;
import java.util.Map;

/**
 * @author malingdi
 * @description 配置类包装注册机和SQL语句
 * @date 2023/12/4 15:07
 */
public class Configuration {

    /**
     * 映射注册机
     */
    protected MapperRegistry mapperRegistry = new MapperRegistry(this);

    /**
     * 映射的语句，存在Map里
     */
    protected final Map<String, MappedStatement> mappedStatements = new HashMap<>();

    public <T> void addMapper(Class<T> type){
        mapperRegistry.addMapper(type);
    }

    public void addMappers(String packageName) {mapperRegistry.addMappers(packageName);}

    public void addMappedStatement(MappedStatement ms) {
        mappedStatements.put(ms.getId(), ms);
    }

    public <T> T getMapper(Class<T> type, SqlSession sqlSession) {return mapperRegistry.getMapper(type, sqlSession);}

    public boolean hasMapper(Class<?> type){ return mapperRegistry.hasMapper(type);}

    public MappedStatement getMappedStatement(String id) {return mappedStatements.get(id);}
}
